unit Options;

{
  PasCalc
  Version 1.2 2011-05-26

  Copyright (C) 2011 KEEPER <yar.keeper@gmail.com>

  This file is part of the PasCalc Calculator

  This unit is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  See the file LICENSE.TXT, included in this distribution,
  for details about the copyright.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ComCtrls, Buttons, StdCtrls, ExtCtrls, ColorBox, UsedTypes, WinVer;

type

  { TfrmOptions }

  TfrmOptions = class(TForm)
    btnCancel: TBitBtn;
    btnSave: TBitBtn;
    btnLangOpen: TButton;
    btnAccept: TButton;
    cbIsHistory: TCheckBox;
    cbIsLog: TCheckBox;
    cbGrid: TColorBox;
    cbGraph: TColorBox;
    cbFloatMode: TComboBox;
    cbComplMode: TComboBox;
    cbCGridStyle: TComboBox;
    cbManyInOne: TCheckBox;
    edLang: TEdit;
    gbCornSett: TGroupBox;
    lblCGridStyle: TLabel;
    lblLang: TLabel;
    lblCornMode: TLabel;
    lblComplPres: TLabel;
    lblCoordSystemColor: TLabel;
    lblGraphColor: TLabel;
    edSize: TLabeledEdit;
    edFract: TLabeledEdit;
    dlgOpenLang: TOpenDialog;
    pcOptions: TPageControl;
    pMain: TTabSheet;
    pDigPres: TTabSheet;
    pGraph: TTabSheet;
    procedure btnCancelClick(Sender: TObject);
    procedure btnLangOpenClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnAcceptClick(Sender: TObject);
    procedure cbFloatModeChange(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
  private
  public
    procedure SetControlsState;
    procedure GetControlsState;
    procedure OptionsLangInit(strs: TStringList);
  end; 

var

  frmOptions: TfrmOptions;
  fOptions: TOptions;
  fLang: ANSIString = '';
  SavePath: ANSIString = '';

implementation

{$i OptLangInit.inc}

{ TfrmOptions }

procedure SetDefaults;
begin
  with fOptions do
  begin
    FloatPres := fpDefault;
    Size := 6;
    Fract := 2;
    GraphColor := clBlack;
    NetColor := clBlack;
    GraphStyle := gsCages;
    IsRad := false;
    ManyInOne := true;
    ComplPres := cpAlg;
    Lang := application.Location + 'russian.lng';
    IsHistory := true;
    IsLogging := true;
  end;
end;

function ExtractName(Str: ANSIString): ANSIString;
var
  i: integer;
  SlashPos: integer;
begin
  for i := Length(Str) downto 1 do
    if Str[i] = '\' then
    begin
      SlashPos := i + 1;
      Break;
    end;
  result := copy(str, SlashPos, length(Str) - i);
end;

procedure SaveToFile;
var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create(SavePath + 'options.dat', fmCreate);
  Stream.Write(fOptions, SizeOf(TOptions));
  Stream.Free;
end;

procedure LoadFromFile;
var
  Stream: TFileStream;
begin
  if not FileExists(SavePath + 'options.dat') then
  begin
    SetDefaults;
    CreateDir(SavePath);
    SaveToFile;
    exit;
  end;
  Stream := TFileStream.Create(SavePath + 'options.dat', fmOpenRead);
  Stream.Read(fOptions, SizeOf(TOptions));
  Stream.Free;
end;

procedure TfrmOptions.btnAcceptClick(Sender: TObject);
var
  StrList: TStringList;
begin
  GetControlsState;
  SaveToFile;
  StrList := TStringList.Create;
  StrList.LoadFromFile(fOptions.Lang);
  OptionsLangInit(StrList);
  StrList.Free;
end;

procedure TfrmOptions.cbFloatModeChange(Sender: TObject);
begin
  edSize.Enabled := cbFloatMode.ItemIndex = 2;
  edFract.Enabled := cbFloatMode.ItemIndex = 2;
end;

procedure TfrmOptions.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  ShowMessage(IntToStr(Key));
  if key = 18 then frmOptions.Repaint;
end;

procedure TfrmOptions.btnSaveClick(Sender: TObject);
var
  StrList: TStringList;
begin
  GetControlsState;
  SaveToFile;
  StrList := TStringList.Create;
  StrList.LoadFromFile(fOptions.Lang);
  OptionsLangInit(StrList);
  StrList.Free;
  frmOptions.Close;
end;

procedure TfrmOptions.btnCancelClick(Sender: TObject);
begin
  frmOptions.Close;
end;

procedure TfrmOptions.btnLangOpenClick(Sender: TObject);
begin
  if dlgOpenLang.Execute then
  begin
    fLang := dlgOpenLang.FileName;
    edLang.Caption := ExtractName(dlgOpenLang.FileName);
  end;
end;

procedure TfrmOptions.FormShow(Sender: TObject);
begin
  SetControlsState
end;

procedure TfrmOptions.GetControlsState;
begin
  fOptions.IsHistory := cbIsHistory.Checked;
  fOptions.IsLogging := cbIsLog.Checked;
  fOptions.Lang := fLang;
  case cbFloatMode.ItemIndex of
    0: fOptions.FloatPres := fpDefault;
    1: fOptions.FloatPres := fpAuto;
    2:
    begin
      fOptions.FloatPres := fpManual;
      fOptions.Size  := StrToInt(edSize.Text);
      fOptions.Fract := StrToInt(edFract.Text);
    end;
  end;
  if cbComplMode.ItemIndex = 0  then fOptions.ComplPres := cpAlg
    else fOptions.ComplPres := cpExp;
  if cbCGridStyle.ItemIndex = 0 then fOptions.GraphStyle := gsCages else
    fOptions.GraphStyle := gsCells;
  fOptions.ManyInOne := cbManyinOne.Checked;
  fOptions.NetColor   := cbGrid.Selected;
  fOptions.GraphColor := cbGraph.Selected;
end;

procedure TfrmOptions.SetControlsState;
begin
  cbIsHistory.Checked := fOptions.IsHistory;
  cbIsLog.Checked := fOptions.IsLogging;
  fLang := fOptions.Lang;
  edLang.Text := ExtractName(fOptions.Lang);
  if fOptions.FloatPres = fpManual then
  begin
    edSize.Enabled := true;
    edFract.Enabled := true;
  end;
  case fOptions.FloatPres of
    fpDefault: cbFloatMode.ItemIndex := 0;
    fpAuto: cbFloatMode.ItemIndex := 1;
    fpManual: cbFloatMode.ItemIndex := 2;
  end;
  if fOptions.GraphStyle = gsCages then cbCGridStyle.ItemIndex := 0 else
    cbCGridStyle.ItemIndex := 1;
  cbManyinOne.Checked := fOptions.ManyInOne;
  edSize.Text := IntToStr(fOptions.Size);
  edFract.Text := IntToStr(fOptions.Fract);
  if fOptions.ComplPres = cpAlg then cbComplMode.ItemIndex := 0
    else cbComplMode.ItemIndex := 1;
  cbGrid.Selected := fOptions.NetColor;
  cbGraph.Selected := fOptions.GraphColor;
end;

initialization
  {$I Options.lrs}
  if (WinVersion = WinVista) or (WinVersion = Win7) then SavePath := GetUserAppDataFolderPath + 'PasCalc\';
  LoadFromFile;
end.

